package com.goldcn.sys.controller;

import com.goldcn.interceptor.CurrentUser;
import com.goldcn.common.BaseCode;
import com.goldcn.common.CommonUtils;
import com.goldcn.common.ResultData;
import com.goldcn.sys.service.SysPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * @Author Tim Peng
 * mail: lmn_022@163.com
 * Date: 2017/6/5
 * Time: 16:54
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/sysPage")
@Api(value = "SysPage",description = "菜单管理")
public class SysPageController {

    @Autowired
    SysPageService sysPageService;

    @ApiOperation(value="查询菜单，根据停用启用状态查询")
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    public Map selectAll(HttpServletRequest request) {
        CurrentUser user = (CurrentUser) request.getAttribute("user");
        Map<String,String> param = CommonUtils.getParameterMap(request);
        return sysPageService.selectAll(param, user);
    }

    @ApiOperation(value="查询启用和停用的菜单")
    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    public Object getAll(@RequestBody com.goldcn.common.RequestParam  params) {
        Map<String,String> param = (Map<String,String>) params.getParams();
        return sysPageService.getAll(param);
    }

    @ApiOperation(value="添加菜单")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody com.goldcn.common.RequestParam  params, HttpServletRequest request){
        try{
            CurrentUser user = (CurrentUser)request.getAttribute("user");
            Map<String,String> map = (Map<String,String>) params.getParams();
            if(user == null)
                return new ResultData(BaseCode.ERROR_CODE102);
            return sysPageService.saveOrUpdate(map,user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResultData(BaseCode.ERROR_CODE103);
    }

    @ApiOperation(value="修改菜单")
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public Object modify(@RequestBody com.goldcn.common.RequestParam  params, HttpServletRequest request){
        return add(params,request);
    }

    @ApiOperation(value="查询菜单")
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public Object getPage(HttpServletRequest request, String pageID) {
        return sysPageService.getPage(pageID);
    }


    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public Object del(@RequestParam String pageID) {
        if(CommonUtils.isEmpty(pageID)){
            return new ResultData<Object>(BaseCode.ERROR_CODE103);
        }
        return sysPageService.del(pageID);
    }

    @RequestMapping(value = "/getPageList",method = RequestMethod.GET)
    public Map getPageList(String pageID){
        return sysPageService.getPageList(pageID);
    }

    @RequestMapping(value = "/getEnabledPageList",method = RequestMethod.GET)
    public Map getEnabledPageList(String type){
        return sysPageService.getEnabledPageList(type);
    }
}
